home *** CD-ROM | disk | FTP | other *** search
- #include "Neural Network.h"
- #include <math.h>
-
- extern NeuralNet * theNet;
- extern DTypeVector yData;
- extern DTypeMatrix XData;
- extern DTypeVector Alpha[];
- extern DTypeMatrix Jac_T;
- extern DTypeVector Pi, Diag;
- extern DTypeVector Resid;
- extern DTypeVector gradSS;
- extern DTypeVector dSquash[];
- extern DTypeMatrix Phi, T2;
-
- double CalcMachEPS();
-
- /***************************************************************************/
- SetupNetDefaults()
- {
- SetupTolerances();
- theNet->OutLayer = 2;
- theNet->Units[0] = 0;
- theNet->Units[1] = 2; /* defaults to two units in first hidden layer */
- theNet->itnlimit = 15;
- theNet->maxstep = 10.0; /* default step limit */
- theNet->maxrelstep = .1; /* default of a 10% max relative step, not used by default HookeJeeves method */
- theNet->usemaxstep = TRUE; /* force use of max step limit, not used by HookeJeeves method */
- theNet->type = SigUB; /* defaults to unbiased Sigma type network */
- theNet->method = HookeJeeves; /* defaults to Hooke & Jeeves direct search method */
- theNet->squash = Logistic;
- theNet->valid = FALSE; /* new net is not estimated yet */
- }
-
- /*------------------
- Allot memory for weight matrices and initialize weight values. Expects the number
- of units in each layer to be already defined.
- */
- AllotInitNewNetWeights()
- {
- int i;
-
- for(i=0; i<theNet->OutLayer; i++) /* output layer has no weight matrix */
- { /* ---- allot memory for Weight matrices ----*/
- AllotDTypeMatrix(&(theNet->W[i]), theNet->Units[i+1], theNet->Units[i]);
- /* for layer i:
- number of columns is number of neurons in layer i,
- number of rows is number of neurons in layer i+1
- */
-
- /*---- initialize parameter values ----*/
- HLock(theNet->W[i].cells);
- InitWeights(&(theNet->W[i]));
- HUnlock(theNet->W[i].cells);
- }
- }
-
- HLockNet()
- {
- int i;
-
- for(i=0; i<theNet->OutLayer; i++)
- { HLock(theNet->W[i].cells);
- }
- }
-
- HUnlockNet()
- {
- int i;
-
- for(i=0; i<theNet->OutLayer; i++)
- { HUnlock(theNet->W[i].cells);
- }
- }
-
- DisplayNet()
- {
- int i;
- printf("Output layer is %d \n",theNet->OutLayer);
- for(i=0; i<=theNet->OutLayer; i++)
- printf("Neurons in layer %d is %d\n",i,theNet->Units[i]);
- for(i=0; i<theNet->OutLayer; i++)
- {
- printf("Weight matrix for layer %d is:\n",i);
- DisplayDTypeMatrix(&theNet->W[i]);
- }
- }
-
- InitWeights(matrix)
- DTypeMatrix * matrix; /* weight matrix to use in network calculation */
- {
- int i,j,k;
- DataType * cell;
- DataType d = .10;
-
- cell = *matrix->cells;
- for(i=0, k=0; i<matrix->rows; i++)
- {
- for(j=0; j<matrix->cols; j++, k++, d = d+.01)
- *(cell + (i*matrix->cols + j)) = d*(DataType)pow(-1,k);
- }
-
- }
-
- SetupTolerances()
- {
- theNet->gradtol = .000001;
- theNet->steptol = .000000000001;
- }
-
-